<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Template Library
*
* @package Template
* @category Libraries
* @author David Blencowe
* @link http://syntaxmonster.net
*
* The template library provides an easy way to manage themes for your website and use multiple views
*/

class Template extends Controller
{
	
	var $CI;
	var $db;
	var $uri;
	var $type;
	var $template;
	var $module;
	
	/** 
	* Auth constructor PHP5
	*
	* @access public
	*/
	
	function __construct()
	{
		$this->CI =& get_instance();
		$CI =& get_instance();
		$this->db = get_instance()->db;
		$this->uri = get_instance()->uri;
		
	}
		
	
	/** 
	* Auth constructor PHP4 support
	*
	* @access public
	*/
	function Template()
	{
		self::__construct();
	}
	
	
	/** 
	* Set the type of theme
	*
	* Should be either 'front' for frontend websites or 'back' for backend admin systems.
	*
	* @access public
	* @param string
	*/
	function type($type)
	{
		$this->type = $type;
	} // type
	
	
	/** 
	* Set the theme name
	*
	* The name of the theme you are using.
	*
	* @access public
	* @param string
	*/
	function set()
	{
		//$this->template = $template;
		$this->db->where('active', '1');
		$get_template = $this->db->get('themes');
		$template = $get_template->row();
		$this->template = $template->name;
		if(!defined('THEMEDIR'))
		{
			define('THEMEDIR_FULL', BASEDIR.'themes/'.$template->name.'/');
			define('THEMEDIR', base_url().'themes/'.$template->name.'/');
		}


	} // set
	
	
	/** 
	* Set the module
	*
	* This will set a module variable so if you are using HMVC, the library will look in the local views/ folder.
	*
	* @access public
	* @param string
	*/
	function module($module)
	{
		$this->module = $module;
	}
	
	/** 
	* Load a view
	*
	* Will be the name of the view you want to load without the .php extension.
	*
	* @access public
	* @param string
	*/
	function load($view, $data = NULL)
	{
		$this->set();
		$site = $this->site();
		if($data !== NULL)
		{
			extract($data, EXTR_PREFIX_SAME, 'forest');
		}
		
		if(file_exists("themes/".$this->template."/".$view.EXT))
		{
			include("themes/".$this->template."/".$view.EXT);			
		}
		elseif(file_exists("modules/".$this->module."/views/".$view.EXT))
		{
			include("modules/".$this->module."/views/".$view.EXT);
		}
		else
		{
			show_error("Page ".$view." can not be found.");
		}

	} // load

	function site($param = NULL)
	{
		$this->db->where('id', '1');
		$get_configuration = $this->db->get("configuration");
		$configuration = $get_configuration->row_array();
		if ($param != NULL)
		{
			return $configuration[''.$param.''];
		}
		else
		{
			return $configuration;
		}
	}

	//Return a pre-formatted Navigation list.
	function front_navigation($config = array())
	{
		$CI =& get_instance();
		$CI->load->model('mdl_nav_helper');
		foreach( $CI->mdl_nav_helper->get_navigation() As $nav )
		{
				$li_class =  $CI->functions->space2underscore($nav['title']); // Turn "Example Text" in to "Example_Text" for styling purpose
				$navigation_item = "<li ";
				foreach ( $config as $key => $value)
				{
					$navigation_item .= $key."='".$value."'";
				}
				$navigation_item .= "><em>";
				$navigation_item .= anchor($nav['fake_route'], $CI->functions->underscore2space($nav['title']) );
				$navigation_item .= "</em></li>";

				echo $navigation_item;
		}
		
		// Section for admin links from modules
		
		// open the current directory
		$dhandle = opendir('modules');
		// define an array to hold the files
		$files = array();

		if ($dhandle)
		{
			// loop through all of the files
			while (false !== ($fname = readdir($dhandle)))
			{
				// if the file is not this file, and does not start with a '.' or '..',
				// then store it for later display
				if (($fname[0] != '.'))
				{
				  // store the filename
				  $files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname;
			  }
		   }
		   // close the directory
		   closedir($dhandle);
		}

		// Now loop through the files, echoing out a new select option for each one
		foreach( $files as $fname )
		{
			$name = ucfirst($fname);

			if( file_exists('./modules/'.$fname.'/front.txt') )				
			{
					
					echo("<li><em>".anchor($fname.'/', $name."")."</em></li>\n");
			}		
		} // foreach	
	}
	
	//Returns an a multidimentional array of navigation items for use with more advanced menus.
	function raw_navigation($config = array() ){
		$CI =& get_instance();
		$CI->load->model('mdl_nav_helper');
		$CI->load->helper('url');
		$i=0;
		
		foreach( $CI->mdl_nav_helper->get_navigation() As $nav )
		{
				$nava = "";
				foreach ( $config as $key => $value)
				{
					$navigation[$i]['url'] = $key['fake_route'];
					$navigation[$i]['title'] = $key['title'];
					$i++;
				}
				$navigation[$i]['title'] = $CI->functions->space2underscore($nav['title']);
				$navigation[$i]['url'] = $nav['fake_route'];
				
				$i++;
		}
		
		// Section for addmin links from modules
		
		// open the current directory
		$dhandle = opendir('modules');
		// define an array to hold the files
		$files = array();

		if ($dhandle)
		{
			// loop through all of the files
			while (false !== ($fname = readdir($dhandle)))
			{
				// if the file is not this file, and does not start with a '.' or '..',
				// then store it for later display
				if (($fname[0] != '.'))
				{
				  // store the filename
				  $files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname;
			  }
		   }
		   // close the directory
		   closedir($dhandle);
		}

		// Now loop through the files, and adding a new element for each one.
		foreach( $files as $fname )
		{
			$name = ucfirst($fname);

			if( file_exists('./modules/'.$fname.'/front.txt') )				
			{
					$navigation[$i]['title'] = $name;
					$navigation[$i]['url'] = $fname.'/';
			}
			$i++;
		} // foreach	
		print_r($navigation);
	}
	
	
	//Returns a Pre-formatted breadcrumbs string.
	function breadcrumbs($config = array())
	{
		$CI =& get_instance();
		$CI->load->model('mdl_nav_helper');
		foreach( $CI->mdl_nav_helper->get_breadcrumbs() As $nav )
		{
				$li_class = preg_replace('/\s\s*/', '_', $nav['title']); // Turn "Example Text" in to "Example_Text" for styling purpose
				$breadcrumbs = '<a ';
				foreach ( $config as $key => $value)
				{
					$breadcrumbs .= $key."='".$value."'";
				}
				$breadcrumbs .= ">";
				$breadcrumbs .= anchor($nav['fake_route'], $nav['title']);

				echo $breadcrumbs;
		}
	}
	
	function breadcrumbs_test($config = array())
	{
		$CI =& get_instance();
		$CI->load->model('mdl_nav_helper');
		$breadcrumbs = $CI->mdl_nav_helper->get_breadcrumbs();
		if ( $breadcrumbs == null ){
			return;
		}
		foreach ( $breadcrumbs as $crumb){
			$underscore_free_text = str_replace("_"," ", $crumb['text']);
			echo "<span class='name'>".anchor($crumb['url'], ucwords($underscore_free_text) )."</span>";
		}
		
		//echo $breadcrumbs;
	}
	
} // Template

/* End of file Template.php */
/* Location: application/libraries */
